package com.edu.kindergarten.server;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.edu.kindergarten.app.MyApplication;
import com.edu.kindergarten.config.Constants;
import com.edu.kindergarten.entity.AccountEntity;
import com.edu.kindergarten.entity.VersionEntity;
import com.edu.kindergarten.provider.AccountTable;
import com.edu.kindergarten.utils.JsonUtils;
import com.edu.kindergarten.utils.LogUtils;

public class HttpServer {
	private static final String TAG = "HttpServer";
	private static HttpServer mServer;
	
	public static HttpServer getInstance(){
		if (mServer == null) {
			synchronized (HttpServer.class) {
				if (mServer == null) {
					mServer = new HttpServer();
				}
			}
		}
		
		return mServer;
	}
	
	public static int testFeedback(){
		
		String url = Constants.URL_FEEDBACK;
		boolean ok = false;
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("feedback", "How about this feedback?"));
			
			JSONObject json = JsonUtils.getJSON(url,params);
			LogUtils.d(TAG,"testFeedback, json:" + json);
			ok = true;
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "zxj,testFeedback," + e.getMessage());
			e.printStackTrace();
		}
		
		Log.d(TAG,"testFeedback, is network ok? " + ok);
		return 1;
	}
	
	public AccountEntity login(AccountEntity from){
		AccountEntity to = new AccountEntity();
		String url = Constants.URL_LOGIN;
		boolean ok = false;
		try {
			/*List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(AccountTable.ACCOUNT, MyUtils.getMD5(from.userName)));
			params.add(new BasicNameValuePair(AccountTable.PASSWORD, MyUtils.getMD5(from.password)));
			params.add(new BasicNameValuePair("appVersion", MyApplication.versionName));
			params.add(new BasicNameValuePair("platform", "Android"));
			params.add(new BasicNameValuePair("osVersion", android.os.Build.VERSION.SDK));

			LogUtils.d(TAG,"login,url:" + url +
					",user name:" + from.userName + 
					",password:" + from.password +
					",os version:" + android.os.Build.VERSION.SDK);
			
			JSONObject json = JsonUtils.getJSON(url,params);
			LogUtils.d(TAG,"login, json:" + json);
			setAccountEntity(from,to,json);
			ok = true;*/
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "zxj,login," + e.getMessage());
			e.printStackTrace();
		}
		
		//dummy start
		try {
			Thread.sleep(1000);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		String password = "123";
		if (/*userName.equals(from.userName) && */password.equals(from.getPassword())) {
			ok = true;
			to.setErrCode(Constants.ACCOUNT_LOGIN_SUCCEED);
			to.setUserName(from.getUserName());
			to.setPassword(from.getPassword());
		}else {
			ok = true;
			to.setErrCode(Constants.ACCOUNT_PASSWORD_WRONG);
		}
		//dummy end
		
		Log.d(TAG,"login, is network ok? " + ok);
		if (!ok) {
			to.setStatus( Constants.NETWORK_ERROR);
		}
		
		return to;
	}
	
	private void setAccountEntity(AccountEntity from,AccountEntity entity,JSONObject json){
		
		try {
			//entity.status = json.getInt(Constants.KEY_STATUS);
			//entity.userName = json.getString(AccountTable.USERNAME);
			entity.setUserName(from.getUserName());
			entity.setPassword(json.getString(AccountTable.PASSWORD));
			entity.setAccessToken(json.getString("accessToken"));
			entity.setId(json.getString("id"));
			entity.setNickName(json.getString("name"));
			entity.setSchoolId(json.getString("schoolId"));
			entity.setSchoolName(json.getString("schoolName"));
			entity.setUserIcon(json.getString("logoUrl"));
			
			ArrayList<AccountEntity.Child> childrens = new ArrayList<AccountEntity.Child>();
			JSONArray children = json.getJSONArray("children");
			int len = children.length();
			for (int i = 0; i < len; i++) {
				AccountEntity.Child child = entity.new Child();
				JSONObject jsonObject = children.getJSONObject(i);
				child.setId(jsonObject.getString("id"));
				child.setName(jsonObject.getString("name"));
				child.setGender(jsonObject.getString("gender"));
				child.setBirthday(jsonObject.getString("birthday"));
				childrens.add(child);
			}
			
			entity.setChildrens(childrens);

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param type parent|teacher
	 * @return
	 */
	public VersionEntity checkVersion(String type){
		VersionEntity entity = new VersionEntity();
		String url = Constants.URL_CHECK_VERSION;
		boolean ok = false;
		try {
			url += "?platform=Android&appVersion=" + MyApplication.versionName + "&app=" + type;
			LogUtils.d(TAG,"checkVersion,url:" + url);
			
			JSONObject json = JsonUtils.getJSON(url);
			LogUtils.d(TAG,"checkVersion, json:" + json);
			setVersionEntity(entity,json);
			ok = true;
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "zxj,checkVersion," + e.getMessage());
			e.printStackTrace();
		}
		
		Log.d(TAG,"checkVersion, is network ok? " + ok);
		if (!ok) {
			entity.setStatus(Constants.NETWORK_ERROR);
		}
		
		return entity;
	}
	
	private void setVersionEntity(VersionEntity entity,JSONObject json){
		
		try {
			entity.setId(json.getString("id"));
			entity.setAppVersion(json.getString("appVersion"));
			entity.setDownloadLink(json.getString("downloadLink"));
			entity.setDescription(json.getString("description"));
			entity.setReleaseNote(json.getString("releaseNote"));
			entity.setPlatform(json.getString("platform"));
			entity.setIfUpdate(json.getBoolean("appUpdate"));
			entity.setIfForceUpdate(json.getBoolean("appForceUpdate"));

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
